<template>
  <!-- 支付 -->
  <van-nav-bar
    title="确认订单"
    left-text="返回"
    left-arrow
    @click-left="
      () => {
        $router.push('/home/ceat');
      }
    "
  />
  <!-- 商品 -->
  <van-swipe-cell v-for="item in usercarts">
    <van-card
      :num="item.number"
      :price="item.goods.goods_price"
      :title="item.goods.goods_name"
      class="goods-card"
      :thumb="item.goods.goods_small_logo"
    >
        <div style="display: flex; width: 100px">
          <!-- 复选框 -->
          <van-checkbox v-model="item.checked"></van-checkbox>
          <!-- 图片 -->
          <van-image
            style="width: 80px"
            :src="item.goods.goods_small_logo" />
        </div>
    </van-card>
  </van-swipe-cell>
  <!-- 提交订单 去支付 -->
   <!-- 动作栏 -->
   <van-submit-bar :price="allprice" button-text="去支付" @submit="onSubmit">
  <!-- <van-checkbox v-model="selectAllCart">全选</van-checkbox> -->
   </van-submit-bar>
</template>

<script setup>
import { computed, ref } from "vue";
import { useCounterStore } from "@/store/ceat";  
const store = useCounterStore();
let usercarts=ref([])
const data=()=>{
    let username = localStorage.getItem("username");
   usercarts.value = store.ceat.filter((item) => item.username == username);
}
data()
// 执行删除
const delItems=(id)=>{
    store.delCart(id)
    // 重新渲染
    data()
}
// 全选计算综合
const selectAllCart=computed ({
    // 获取
    get(){
        if(usercarts.value.length==0){
            return false
        }else{
            return usercarts.value.every(item=>item.checked)
        }
    },
    set(val){

        // 复选所有状态
        usercarts.value.forEach(item=>item.checked=val)
    }
})

// 总价
const allprice =computed(()=>{
    let allprice=0
    // 随机选中的商品
    let cartok=usercarts.value.filter(item=>item.checked)
    cartok.forEach(item=>{
        allprice+=item.number*item.goods.goods_price
    })
    return allprice*100
})
// 跳转
import { useRouter } from 'vue-router'
let router = new useRouter()
const onSubmit = ()=>{
  router.push('/pay')
}
</script>

<style>
</style>